/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package XML;

import com.thoughtworks.xstream.XStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import objetos.Arma;
import objetos.personajes.*;

/** @author Administrador */
public class XML_Parser {

    public void preLoadSerializacionPersonajes() {
	ArrayList<Personaje> personajes = new ArrayList<Personaje>();
	personajes.add(new PersonajePrincipal());
	personajes.add(new Rabona());
	personajes.add(new Indigena());
	personajes.add(new SoldadoDeInfanteria());
	personajes.add(new SoldadoDeCaballeria());
	personajes.add(new Artillero());

	XStream xs = new XStream();
	String cadenaGestor = xs.toXML(personajes);
	try {
	    FileWriter fw = new FileWriter("config/personajes.xml");
	    fw.write(cadenaGestor);
	    fw.close();
	} catch (IOException e) {
	    System.out.println(e.toString());
	}
    }

    public void preLoadSerializacionArmas() {
	ArrayList<Arma> armas = new ArrayList<Arma>();

	armas.add(new Arma(4, 0, "escudo"));
	armas.add(new Arma(4, 1, "piedras"));
	armas.add(new Arma(4, 1, "rejones"));
	armas.add(new Arma(4, 1, "hondas"));
	armas.add(new Arma(3, 0, "escudo"));
	armas.add(new Arma(3, 1, "fusil"));
	armas.add(new Arma(3, 1, "bayoneta"));
	armas.add(new Arma(2, 0, "escudo"));
	armas.add(new Arma(2, 1, "lanza"));
	armas.add(new Arma(2, 1, "caballeria"));
	armas.add(new Arma(1, 0, "escudo"));
	armas.add(new Arma(1, 1, "cañon"));

	XStream xs = new XStream();
	String cadenaGestor = xs.toXML(armas);
	try {
	    FileWriter fw = new FileWriter("config/armas.xml");
	    fw.write(cadenaGestor);
	    fw.close();
	} catch (IOException e) {
	    System.out.println(e.toString());
	}
    }

    public void preLoadSerializacionAldea() {
	XStream xs = new XStream();
	String cadenaGestor = xs.toXML(new AldeaXML());
	try {
	    FileWriter fw = new FileWriter("config/aldea.xml");
	    fw.write(cadenaGestor);
	    fw.close();
	} catch (IOException e) {
	    System.out.println(e.toString());
	}
    }

    public void preLoadSerializacionMapa(int nivel) {
	MapaXML mapa = new MapaXML();
	mapa.nivel = nivel;

	switch (nivel) {

	    case 0:
		mapa.nombre = "Batalla del Alto Peru";
		mapa.M = 50;
		mapa.N = 20;
//	public ObjetoXML(String clase, int x, int y, boolean principal, int bando, int tipoObstaculo)
		int coordX1[] = {9, 10, 11, 38, 39, 30, 39, 40, 41, 42, 33, 34, 35, 36, 37, 5, 6, 7, 38, 5, 6, 7, 17, 16, 14, 15, 12, 13, 14, 31, 32, 33, 11, 10, 9, 3, 2, 7, 8};
		int coordY1[] = {1, 1, 1, 1, 1, 1, 5, 5, 5, 5, 6, 7, 7, 7, 8, 9, 9, 9, 9, 10, 10, 10, 13, 14, 15, 15, 16, 16, 16, 16, 16, 16, 17, 18, 19, 5, 3, 3, 6};
		int obs1[] = {1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 2, 2, 2, 0, 2, 2, 2, 0, 0, 2, 0, 0, 0, 0, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0};

		for (int i = 0; i < obs1.length; i++) {
		    mapa.objetos.add(new ObjetoXML("Obstaculo", coordX1[i], coordY1[i], false, -1, obs1[i], -1));
		}

		mapa.objetos.add(new ObjetoXML("Personaje", 6, 3, true, 0, -1, 0));
		mapa.objetos.add(new ObjetoXML("Personaje", 9, 2, true, 1, -1, 0));

		mapa.objetos.add(new ObjetoXML("Aldea", 0, 0, false, 0, -1, -1));
		mapa.objetos.add(new ObjetoXML("Aldea", 49, 0, false, 0, -1, -1));
		mapa.objetos.add(new ObjetoXML("Aldea", 49, 19, false, 0, -1, -1));
		mapa.objetos.add(new ObjetoXML("Aldea", 0, 19, false, 0, -1, -1));
		mapa.objetos.add(new ObjetoXML("Aldea", 24, 9, false, 0, -1, -1));
		break;

	    case 1:
		mapa.nombre = "Batalla de Umachiri";
		mapa.M = 60;
		mapa.N = 25;
		int coordX2[] = {9, 10, 11, 38, 39, 40, 54, 55, 54, 55, 8, 9, 10, 8, 9, 39, 40, 41, 42, 8, 33, 34, 35, 36, 37, 5, 6, 7, 38, 5, 6, 7, 51, 52, 49, 50, 17, 47, 48, 16, 14, 15, 12, 13, 14, 31, 32, 33, 11, 10, 46, 9, 45, 46, 47, 20, 19, 20, 21, 18, 19, 20, 21, 22, 23};
		int coordY2[] = {1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 4, 4, 4, 5, 5, 5, 5, 5, 5, 6, 6, 7, 7, 7, 8, 9, 9, 9, 9, 10, 10, 10, 11, 11, 12, 12, 13, 13, 13, 14, 15, 15, 16, 16, 16, 16, 16, 16, 17, 18, 18, 19, 19, 19, 19, 21, 22, 22, 22, 23, 23, 23, 23, 23};
		int obs2[] = {1, 1, 1, 0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 2, 0, 0, 0, 0, 0, 2, 2, 2, 0, 2, 2, 2, 1, 1, 1, 1, 0, 1, 1, 0, 2, 0, 0, 0, 0, 2, 2, 2, 0, 0, 2, 0, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1};

		for (int i = 0; i < obs2.length; i++) {
		    mapa.objetos.add(new ObjetoXML("Obstaculo", coordX2[i], coordY2[i], false, -1, obs2[i],-1));
		}

		mapa.objetos.add(new ObjetoXML("Batallon", 6, 3, true, 0, -1, 0));
		mapa.objetos.add(new ObjetoXML("Batallon", 9, 2, true, 1, -1, 0));

		mapa.objetos.add(new ObjetoXML("Aldea", 0, 0, false, 0, -1, -1));
		mapa.objetos.add(new ObjetoXML("Aldea", 59, 0, false, 0, -1, -1));
		mapa.objetos.add(new ObjetoXML("Aldea", 59, 24, false, 0, -1, -1));
		mapa.objetos.add(new ObjetoXML("Aldea", 0, 24, false, 0, -1, -1));
		mapa.objetos.add(new ObjetoXML("Aldea", 29, 12, false, 0, -1, -1));
		break;
	}

	XStream xs = new XStream();
	String cadenaGestor = xs.toXML(mapa);
	try {
	    FileWriter fw = new FileWriter("config/mapa" + nivel + ".xml");
	    fw.write(cadenaGestor);
	    fw.close();
	} catch (IOException e) {
	    System.out.println(e.toString());
	}



    }

    public void preLoadBatallones(int bando, int nivel) {
	ArrayList<PersonajeXML> personajes = new ArrayList<PersonajeXML>();
	// public ArmaXML(String nombreArma, int tipoArma)


	PersonajeXML personajePrincipal = new PersonajeXML(0, 1, true);
	personajePrincipal.armas.add(new ArmaXML("escudo", 0));
	personajePrincipal.armas.add(new ArmaXML("lanzon", 0));
	personajePrincipal.armas.add(new ArmaXML("lanzon", 0));

	int numIndigenas;
	int numArtilleros;
	int numCaballeros;
	int numInfantes;
	int numRabonas;

	if (nivel == 0) {
	    numIndigenas = bando == 0 ? 20 : 0;
	} else {
	    numIndigenas = bando == 0 ? 30 : 0;
	}

	if (nivel == 0) {
	    numArtilleros = bando == 0 ? 0 : 7;
	} else {
	    numArtilleros = bando == 0 ? 0 : 9;
	}

	if (nivel == 0) {
	    numCaballeros = bando == 0 ? 0 : 4;
	} else {
	    numCaballeros = bando == 0 ? 0 : 5;
	}

	if (nivel == 0) {
	    numInfantes = bando == 0 ? 0 : 5;
	} else {
	    numInfantes = bando == 0 ? 0 : 6;
	}

	if (nivel == 0) {
	    numRabonas = bando == 0 ? 8 : 5;
	} else {
	    numRabonas = bando == 0 ? 10 : 7;
	}

// public PersonajeXML(int tipoPersonaje, int cantidad, boolean principal)
	PersonajeXML indigena = new PersonajeXML(4, numIndigenas, false);
	PersonajeXML infantes = new PersonajeXML(3, numInfantes, false);
	PersonajeXML caballeros = new PersonajeXML(2, numCaballeros, false);
	PersonajeXML artilleros = new PersonajeXML(1, numArtilleros, false);
	PersonajeXML rabonas = new PersonajeXML(5, numRabonas, false);

// public ArmaXML(String nombreArma, int tipoArma)

	indigena.armas.add(new ArmaXML("piedras", 1));
	indigena.armas.add(new ArmaXML("rejones", 1));
	indigena.armas.add(new ArmaXML("escudo", 0));

	infantes.armas.add(new ArmaXML("fusil", 1));
	infantes.armas.add(new ArmaXML("escudo", 0));

	caballeros.armas.add(new ArmaXML("caballeria", 1));
	caballeros.armas.add(new ArmaXML("escudo", 0));

	artilleros.armas.add(new ArmaXML("cañon", 1));
	artilleros.armas.add(new ArmaXML("escudo", 0));

	personajes.add(personajePrincipal);
	personajes.add(indigena);
	personajes.add(caballeros);
	personajes.add(infantes);
	personajes.add(artilleros);
	personajes.add(rabonas);

	XStream xs = new XStream();
	String cadenaGestor = xs.toXML(personajes);
	try {
	    FileWriter fw = new FileWriter("config/batallonN" + nivel + "B" + bando + ".xml");
	    fw.write(cadenaGestor);
	    fw.close();
	} catch (IOException e) {
	    System.out.println(e.toString());
	}
    }

	public void preLoadInfoGeneral() {
	XStream xs = new XStream();
	String cadenaGestor = xs.toXML(new InfoGeneral());
	try {
	    FileWriter fw = new FileWriter("config/infogeneral.xml");
	    fw.write(cadenaGestor);
	    fw.close();
	} catch (IOException e) {
	    System.out.println(e.toString());
	}
    }
}
